import {
  AuthService
} from "./services/auth_service";
// const ald = require('./utils/ald-stat');

// app.js
App({
  onLaunch(options) {
    // 登录
    const tokenExpires = wx.getStorageSync('tokenExpires') ?? 0;
    const userInfo = wx.getStorageSync('userInfo');

    if (userInfo && tokenExpires > Date.now()) {
      this.globalData.tokenExpires = tokenExpires;
      this.globalData.userInfo = JSON.parse(userInfo);
    } else {
      wx.login({
        success: res => {
          console.log(res)
          let authService = new AuthService();
          authService.login(res.code).then(res => {
            this.globalData.userInfo = res.data;
            this.globalData.tokenExpires = Date.now() + 3600 * 24 * 3 * 1000;
            wx.setStorageSync('tokenExpires', Date.now() + 3600 * 24 * 3 * 1000);
            wx.setStorageSync('userInfo', JSON.stringify(res.data));
          });
        }
      })
    }
    
    // 从缓存中读取用户地区
    this.globalData.city = wx.getStorageSync('city') || '深圳市';
  },
  globalData: {
    userInfo: {},
    city: "",
    tokenExpires: null
  }
})